package utils;

import DAO.ItemsDAO;
import DAO.OrdersDAO;
import DAO.UserDAO;
import DAO.impl.ItemsDAOImpl;
import DAO.impl.OrderDAOImpl;
import DAO.impl.UserDAOImpl;
import bean.Items;
import bean.Orders;
import bean.Users;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

/**
 * @author 栋
 * @date 2022/11/15
 * @description
 */
public class BianHaoUtils {
    public static String getUserId() {
        StringBuilder id=new StringBuilder("U");
        Date date= Date.valueOf(LocalDate.now());
        String s = date.toString();
        String[] split = s.split("-");
        for (String s1 : split) {
            id.append(s1);
        }

        Connection conn=null;
        UserDAO userDAO=new UserDAOImpl();
        try {
            conn = JdbcUtils.getConnection();
            List<Users> all_users = userDAO.get_all_users(conn);
            Integer end=1;
            if(!all_users.isEmpty()){
                int size = all_users.size();
                Users users = all_users.get(size-1);
                if(users.getUser_id().startsWith(id.toString())){
                    String substring = users.getUser_id().substring(9, 14);
                    end = Integer.valueOf(substring)+1;
                }
            }
            String endprefix = String.format("%05d",end);
            id.append(endprefix);
            String ids = id.toString();
            return ids;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.closeResoure(conn);
        }

        return null;
    }

    public static String getItemId() {
        StringBuilder id=new StringBuilder("I");
        Date date= Date.valueOf(LocalDate.now());
        String s = date.toString();
        String[] split = s.split("-");
        for (String s1 : split) {
            id.append(s1);
        }
        Connection conn=null;
        ItemsDAO itemsDAO=new ItemsDAOImpl();
        try {
            conn = JdbcUtils.getConnection();
            List<Items> all_items = itemsDAO.get_all_items(conn);
            Integer end=1;
            if(!all_items.isEmpty()){
                int size = all_items.size();
                Items items = all_items.get(size-1);
                if(items.getItem_id().startsWith(id.toString())){
                    String substring = items.getItem_id().substring(9, 14);
                    end = Integer.valueOf(substring)+1;
                }
            }
            String endprefix = String.format("%05d",end);
            id.append(endprefix);
            String ids = id.toString();
            return ids;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.closeResoure(conn);
        }

        return null;
    }

    public static String getOrderId(){
        StringBuilder id=new StringBuilder("ORDER");
        Date date= Date.valueOf(LocalDate.now());
        String s = date.toString();
        String[] split = s.split("-");
        for (String s1 : split) {
            id.append(s1);
        }
        Connection conn=null;
        OrdersDAO ordersDAO=new OrderDAOImpl();
        try {
            conn = JdbcUtils.getConnection();
            List<Orders>orders = ordersDAO.get_all_orders(conn);
            Integer end=1;
            if(!orders.isEmpty()){
                int size = orders.size();
                Orders order = orders.get(size-1);
                if(order.getOrder_id().startsWith(id.toString())){
                    String substring = order.getOrder_id().substring(13, 18);
                    end = Integer.valueOf(substring)+1;
                }
            }
            String endprefix = String.format("%05d",end);
            id.append(endprefix);
            String ids = id.toString();
            return ids;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JdbcUtils.closeResoure(conn);
        }

        return null;
    }
}
